<?php

namespace app\api\controller;
use data\Json;
use think\Request;
use think\Cache;
use think\Log;
use JPush\Client as JPush;
use controller\Easemob;
use app\common\model\Travel as TravelModel;
use app\common\model\TravelMember;
use app\common\model\Activity;
/**
 * 用户api接口
 */
class User extends Base
{
    protected $user_model = null;
    protected $token = null;
    protected $user = null;

    public function _initialize()
    {
        $this->user_model = model('User');
        $this->token = request()->header('token');
        //$this->user = $this->user_model->getUserInfo($this->token);
        $this->user = $this->user_model->get('2');
    }

    // 个人中心首页
    public function center()
    {
        if(!$this->user)return Json::json_send(1);
        $communityList = [];
        foreach (explode(',', $this->user['community_ids']) as $k => $v) {
            $community = model('Community')->get($v);
            $communityList[] = ['commmunity_id'=>$community['id'],'community_name'=>$community['name']];
        }
        $data = [
            'id' =>  $this->user['id'],
            'avatar' =>  $this->user['avatar'],
            'name' =>  $this->user['name'],
            'integration' =>  $this->user['integration'],// 活动收入
            'communities' =>  $communityList,// 活动收入
        ];
        return Json::json_send(0,'',$data);
    }

    // 充值
    public function recharge()
    {
        if(!$this->user)return Json::json_send(1);
        $param = [
            'good_id'   => $this->user['id'],
            'total_fee' => input('fee',0),
            'title'     => '充值',
            'channel'   => input('channel','ALI_APP'),//"ALI_APP";
            'type'      => 5,//"充值";
        ];
        $this->redirect(url('pay/index', $param));
    }

     // 提现
    public function withdrawals()
    {
        if(!$this->user)return Json::json_send(1);
        $param = [
            'good_id'   => $this->user['id'],
            'total_fee' => input('fee',0),
            'title'     => '提现',
            'channel'   => input('channel','ALI_APP'),//"ALI_APP";
            'type'      => 6,//"提现";
        ];
        // 生成转账订单
        $order_id = action('pay/prepay',$param);
        // 申请转账
        $this->redirect(url('pay/transfer', ['order_id'=>$order_id,'type'=>'1']));
    }


    // 个人资料
    public function info(Request $request)
    {
        if(!$this->user)return Json::json_send(1);
        if($request->isPost())
        {
            $data = input();
            if(request()->file('avatar'))
            {
                $data['avatar'] = $this->upload(request()->file('avatar'));
            }
            $result = $this->user_model->isUpdate(true)->allowField(true)->save($data,['id'=>$this->user['id']]);
            if( $result === false)
            {   
                return Json::json_send(9);
            }

            if($data['complete'] == 1 && $this->user['complete'] == 0){
                $this->user_model->where(['id'=>$this->user['id']])->setInc('integration', 5);
                $logData = [
                    'user_id' => $this->user['id'],
                    'desc'    => '完善个人信息',
                    'type'    => 0,
                    'integration' =>50,
                ];
                model('IntegrationLog')->save($logData);
            }
            $this->user = $this->user_model->get($this->user['id']);
        }
        $data = [
            'id'       =>  $this->user['id'],
            'name'     =>  $this->user['name'],
            'avatar'   =>  $this->user['avatar'],
            'mobile'   =>  $this->user['mobile'],
            'area'     =>  $this->user['area'],
            'company'  =>  $this->user['company'],
            'post'     =>  $this->user['post'], 
            'complete' =>  $this->user['complete'], 
        ];
        return Json::json_send(0,'',$data);
    }

    // 极光推送消息列表
    public function messages()
    {
        if(!$this->user)return Json::json_send(1);
        $list = model('JpushData')->field('msg,createtime')->where(['type'=>2,'alias'=>$this->user['mobile']])->select();
        return Json::json_send(0,'',$list);
    }

     // 积分明细
    public function integration()
    {
        if(!$this->user)return Json::json_send(1);
        $integrationList = model('IntegrationLog')->where(['user_id'=>$this->user['id']])->select();
        $data=[];
        foreach ($integrationList as $k => $v) {
            $data[] = [
            'id' =>  $this->user['id'],
            'sum' =>  $this->user['integration'],
            'desc' =>  $v['desc'],
            'type' =>  $v['type'],
            'integration' =>  $v['integration'],
            'createtime' =>   $v['create_time'],
        ];
        }
        return Json::json_send(0,'',$data);
    }

    // 签到
    public function checkin()
    {
        if(!$this->user)return Json::json_send(1);
        $validate = new Validate([
            ['travel_id','require','活动id不能为空'],
            ['user_id','require','会员id不能为空'],
        ]);

        $travel_id = $data['travel_id'] = input('travelid');
        $user_id = $data['user_id'] = $this->user['id'];

        if (!$validate->check($data)) return Json::json_send(4);

        $travelmember = model('TravelMember')->where(['travel_id'=>$travel_id,'member_id'=>$this->user['id']])->find();
        if(!$travelmember) return Json::json_send(10012);
        if($travelmember['is_checkin']) return Json::json_send(10013);

        $data['check_time'] = time();
        $data['is_checkin'] = 1;
        $result = model('TravelMember')->where('id',$travelmember['id'])->update($data);
        if($result !== false){
            return Json::json_send(0,"签到成功");
        }
        return Json::json_send(10014);
    }

    // 会员加入社群支付会费
    public function auth(Request $request)
    {
        if(!$this->user)return Json::json_send(1);
        if($request->isPost())
        {
            $commmunity_id = input('community_id',0);
            $communityMember = model('CommunityMember')->where(['community_id'=>$commmunity_id,'user_id'=>$this->user['id']])->find();
            $param = [
                'good_id'   => $communityMember['id'],
                'total_fee' => $communityMember['fee'],
                'title'     => '支付会费',
                'channel'   => input('channel','ALI_APP'),//"ALI_APP";
                'type'      => 2,//"ALI_APP";
            ];
            $this->redirect(url('pay/index', $param));
        }
    }

    // 社群
    public function groups(Request $request)
    {
        if(!$this->user)return Json::json_send(1);

        if($request->isPost())
        {
            $data = input();
            $community = model('Community')->get(input('community_id'));
            $data['createtime'] = time();
            $data['user_id'] = $this->user['id'];
            $result = model('CommunityMember')->allowField(true)->save($data);
            if($result !== false){
                $this->user_model->addGroupMember($community['hx_groupid'],$this->user['hx_username']);
                return Json::json_send(0);
            }
            return Json::json_send(9);
        }

        if(input('community_id'))
        {
            $community_id = input('community_id');
            $community_user = model('CommunityMember')->alias('cm')
                                ->join('user c','cm.community_id=c.id')
                                ->where(['cm.community_id'=>$community_id,'cm.user_id'=>$this->user['id']])
                                ->find();
            $return = [
                'user_id'     => $this->user['id'],
                'username'    => $this->user['name'],
                'community_id'=>$community_id,
                'avatar'      => $this->user['avatar'],
                'community_name' => $community_user['name'],
                'community_avatar' =>  $community_user['avatar'],
                'person'           =>  $community_user['person'],
                'mobile'           =>  $community_user['mobile'],
                'scale'            =>  $community_user['scale'],
                'setup_time' =>  $community_user['setup_time'],
                'setup_addr' =>  $community_user['setup_addr'],
                'is_pay'     =>  $community_user['is_pay'],
                'year_fee'   =>  $community_user['year_fee'],
                'city'       =>  $community_user['city'],
                'website'    =>  $community_user['website'],
                'wechat'     =>  $community_user['website'],
                'is_company' =>  $community_user['is_company'],
                'heldsdata'  =>  model('Community')->getHeldsdataTextAttr($community_user['heldsdata'],$community_user),// 活动收入
                'complete'   =>  $community_user['complete'],
                'entrytime'   => $community_user['entrytime'],
                'expiretime'  => $community_user['expiretime'],
                'paid'        => $community_user['paid'],
                'state'       => $community_user['state'],
                'is_pay'      => $community_user['is_pay'],
                'year_fee'    => $community_user['year_fee'],

            ];
            return Json::json_send(0,'',$return);
        }


        $user = $this->user_model->with('Community')->where(['id'=>$this->user['id']])->find();
        $return = [];
        $return['id'] = $user['id'];
        $return['hx_username'] = $user['hx_username'];
        $return['hx_password'] = $user['hx_password'];
        foreach ($user['community'] as $k => $v) {
            $return['communities'][] = [
                'community_id'    => $v['pivot']['community_id'],
                'community_name'  => $v['name'],
                'community_avatar'=> $v['avatar'],
                'hx_groupid'      => $v['hx_groupid'],
                'state'           => $v['pivot']['state'],
                'flag'            => 0,
            ];
        }
        return Json::json_send(0,'',$return);

    }

    // 所有社群
    public function grouplist()
    {
        $map['state'] = 1;
        $map['role'] = 1;
        if($this->user)
        {
            $communityList = model('CommunityMember')->where(['user_id'=>$this->user['id']])->column('community_id');
            $map['id'] = ['not in',implode(',', $communityList)];
        }

        $grouplist = model('Community')->where($map)->select();
        $return = [];
        foreach ($grouplist as $k => $v) {
            $return[] = ['id'=>$v['id'],'name'=>$v['name'],'is_pay'=>$v['is_pay']];
        }
        return Json::json_send(0,'',$return);
    }

    // 社群成员
    public function groupmember(Request $request)
    {
        if(!$this->user)return Json::json_send(1);

        $community_id = input('community_id');
        $community = model('Community')->get($community_id);

        if(input('member_id'))
        {
            $member_id =  input('member_id');
            $info   =  model('CommunityMember')->alias('cm')
                                ->join('user c','cm.user_id=c.id')
                                ->where(['cm.user_id'=>$member_id,'community_id'=>$community_id])
                                ->find();
            $is_friend = 0;
            if(model('friends')->where(['user_id'=>$this->user['id'],'friend_id'=>$member_id])->whereOr(['friend_id'=>$this->user['id'],'user_id'=>$member_id])->find())
            {
                $is_friend = 1;
            }
            $memberCommunities = model('CommunityMember')->alias('cm')
                                ->join('user u','cm.community_id=u.id')
                                ->field('cm.community_id,u.name')
                                ->where(['cm.user_id'=>$member_id])
                                ->select();
            $return = [
                'user_id'  => $member_id,
                'username' => $info['name'],
                'avatar'   => $info['avatar'],
                'mobile'   => $info['mobile'],
                'entrytime'    => $info['entrytime'],
                'expiretime'   => $info['expiretime'],
                'lastday'      => count_days($info['expiretime'],time()),
                'year_fee'     => $info['year_fee'],
                'area'         => $info['area'],
                'company'      => $info['company'],
                'post'         => $info['post'],
                'state'        => $info['state'],
                'is_friend'    => $is_friend,
                'communitylist'   => $memberCommunities
            ];
            return Json::json_send(0,'',$return);
        }

        $community_users = model('CommunityMember')->alias('cm')
                                ->join('user c','cm.user_id=c.id')
                                ->where(['cm.community_id'=>$community_id])
                                ->select();
        $return = [];
        $return['count']=count($community_users);
        foreach ($community_users as $k => $v) {
            $return['list'][]=[
                'user_id'=>$v['user_id'],
                'avatar'=>$v['avatar'],
                //'hx_username'=>$v['hx_username'],
                'community_id'=>$v['community_id'],
                'username'=>$v['name'],
                'state'=>$v['state'],
                'disturb' => $v['disturb']
            ];
        }
        $return['notice']  = $community['group_notice'];
        return Json::json_send(0,'',$return);
    }

    // 添加记录
    public function records()
    {
        if(!$this->user)return Json::json_send(1);
        $records = model('CommunityMember')->alias('cm')
                                ->join('user c','cm.community_id=c.id')
                                ->where(['cm.user_id'=>$this->user['id']])
                                ->select();
        $return = [];
        foreach ($records as $k => $v) {
            $return[] = [
                'community_id'=>$v['community_id'],
                'community_name'=>$v['name'],
                'paid'=>$v['paid'],
                'year_fee'=>$v['year_fee'],
                'entrytime'=>$v['entrytime'],
                'createtime'=>$v['createtime']
            ];
        }
        return Json::json_send(0,'',$return);
    }

    // 我的好友
    public function friends(Request $request)
    {
        if(!$this->user)return Json::json_send(1);
        if($request->isPost())
        {
            if(input('id'))
            {
                $data = input();
                $state = input('state',0);
                $result = model('Friends')->isUpdate(true)->save($data);
                if($result !== false)
                {
                    if($state == 1)
                    {
                        $friend = model('Friends')->alias('f')
                                    ->join('user u','f.user_id = u.id')
                                    ->where(['f.id'=> input('id')])
                                    ->find();
                        $this->user_model->addFriend($this->user['hx_username'],$friend['hx_username']);
                    }
                    return Json::json_send(0);
                }
                return Json::json_send(9);
            }

            $friend_id = input('friend_id');
            $friend = model('User')->get($friend_id);
            $data = [
                'user_id'=>$this->user['id'],
                'friend_id'=>$friend_id
            ];
            $info = model('Friends')->where(['user_id'=>$this->user['id'],'friend_id'=>$friend_id,'state'=>0])->find();
            if($info)
            {
                $data['id'] = $info['id'];
                $result = model('Friends')->isUpdate(true)->save($data);
            }
            else
            {
                $result = model('Friends')->save($data);
            }

            if($result !== false)
            {
                return Json::json_send(0);
            }
            return Json::json_send(9);
        }

        if(input('friend_id'))
        {
            $member_id =  input('friend_id');
            $info   =  $this->user_model->get($member_id);
            $memberCommunities = model('CommunityMember')->alias('cm')
                                ->join('user u','cm.community_id=u.id')
                                ->field('cm.community_id,u.name')
                                ->where(['cm.user_id'=>$member_id])
                                ->select();

            $return = [
                'user_id'  => $member_id,
                'username' => $info['name'],
                'avatar'   => $info['avatar'],
                'mobile'   => $info['mobile'],
                'area'         => $info['area'],
                'company'      => $info['company'],
                'post'         => $info['post'],
                'state'        => $info['state'],
                'communitylist' => $memberCommunities
            ];

            return Json::json_send(0,'',$return);
        }

        if(input('flag'))
        {
            $map['friend_id'] = $this->user['id'];
            $map['createtime'] = ['>',time()-30*24*3600];
            $friends = model('Friends')->where($map)->select();
            $result = [];
            foreach ($friends as $k => $v) {
                $user = model('User')->get($v['user_id']);
                $result[] = [
                    'id'         =>  $v['id'],
                    'user_id'    =>  $user['id'],
                    'username'   =>  $user['name'],
                    'avatar'     =>  $user['avatar'],
                    'state'      =>  $v['state'],
                    'createtime' =>  $v['createtime']
                ];
            }
        }
        else
        {
            $result = $this->user_model->showFriends($this->user['hx_username']);
        }
        return Json::json_send(0,'',$result);
    }

    // 小圈子
    public function quan(Request $request)
    {
        if(!$this->user)return Json::json_send(1);
        if($request->isPost())
        {
            if(input('quan_id'))
            {
                $data = input();
                $result = model('Quan')->isUpdate(true)->allowField(true)->save($data,['id'=>input('quan_id')]);
                if($result !== false)
                {
                    return Json::json_send(0);
                }
                return Json::json_send(9);
            }

            $data['hx_groupid'] = $this->user_model->createGroup($this->user['hx_username']);
            $data['user_id'] = $this->user['id']; 
            if(model('Quan')->isUpdate(false)->allowField(true)->save($data))
            {
                model('QuanFriends')->isUpdate(false)->save(['quan_id'=>model('Quan')->id,'user_id'=>$this->user['id']]);
                $friend_ids = input('friend_ids');
                $list = [];
                foreach (explode(',', $friend_ids) as $k => $v) {
                    $list[] = ['quan_id'=>model('Quan')->id,'user_id'=>$v];
                }
                $result = model('QuanFriends')->isUpdate(false)->saveAll($list);
                if($result !== false)
                {
                    return Json::json_send(0);
                }
                return Json::json_send(9);
            }
            return Json::json_send(9);
        }

        // 获取群成员
        if(input('quan_id'))
        {
            $quan_id = input('quan_id',0);
            $quan = model('Quan')->get($quan_id);

            if(!empty(input('member_id')))
            {
                $member_id = input('member_id');
                $info = model('QuanFriends')->alias('qf')
                                ->join('User u','qf.user_id=u.id')
                                ->field('*,qf.createtime as entrytime')
                                ->where(['quan_id'=>$quan_id,'user_id'=>$member_id])
                                ->find();
                $is_friend = 0;
                if(model('friends')->where(['user_id'=>$this->user['id'],'friend_id'=>$member_id])->whereOr(['friend_id'=>$this->user['id'],'user_id'=>$member_id])->find())
                {
                    $is_friend = 1;
                }
                $memberCommunities = model('CommunityMember')->alias('cm')
                                    ->join('user u','cm.community_id=u.id')
                                    ->field('cm.community_id,u.name')
                                    ->where(['cm.user_id'=>$member_id])
                                    ->select();
                $return = [
                    'user_id'  => $member_id,
                    'username' => $info['name'],
                    'avatar'   => $info['avatar'],
                    'mobile'   => $info['mobile'],
                    'createtime'   => $info['entrytime'],
                    'area'         => $info['area'],
                    'company'      => $info['company'],
                    'post'         => $info['post'],
                    'state'        => $info['state'],
                    'is_friend'    => $is_friend,
                    'communitylist'   => $memberCommunities
                ];
                return Json::json_send(0,'',$return);
            }

            $memberList = model('QuanFriends')->alias('cm')
                                ->join('user c','cm.user_id=c.id')
                                ->where(['cm.quan_id'=>$quan_id])
                                ->select();
            $return = [];
            $return['count']  = count($memberList);
            $name = [];
            foreach ($memberList as $k => $v) {
                array_push($name, $v['name']);
                $return['list'][]=[
                    'user_id'=>$v['user_id'],
                    'avatar' =>$v['avatar'],
                    //'hx_username'=>$v['hx_username'],
                    'community_id'=>$v['quan_id'],
                    'username'=>$v['name'],
                   // 'state'=>$v['state']
                ];
            }
            $return['notice']  = empty($quan['name'])?implode(',', $name):$quan['name'];
            $return['disturb'] = $quan['disturb'];
            return Json::json_send(0,'',$return);



           /* $friends = model('QuanFriends')->where(['quan_id'=>input('quan_id')])->select();
            $return = [];
            foreach ($friends as $k => $v) {
                $user = model('User')->get($v['user_id']);
                $return[] = ['user_id'=>$user['id'],'username'=>$user['name'],'avatar'=>$user['avatar']];
            }
            return Json::json_send(0,'',$return);*/
        }

        $quanList = model('QuanFriends')->all(['user_id'=>$this->user['id']]);
        $return = [];
        foreach ($quanList as $k => $v) {
            $quan = model('Quan')->get($v['quan_id']);
            $friends = model('QuanFriends')->alias('qf')
                       ->join('user u','qf.user_id=u.id')->where(['quan_id'=>$v['quan_id']])->select();
            $name = [];
            foreach ($friends as $fk => $fv) {
               array_push($name, $fv['name']);
            }
            $quan['name'] = empty($quan['name'])?implode(',', $name):$quan['name'];
            $return[] = ['community_id'=>$quan['id'],'community_name'=>$quan['name'],'hx_groupid'=>$quan['hx_groupid'],'flag'=>1];
        }
        return Json::json_send(0,'',$return);
    }

    // 分享行程
    public function share()
    {
        if(!$this->user) return Json::json_send(1);
        $travel_id = input('travel_id',0);
       
        $type = TravelModel::where(['id'=>$travel_id])->value('type');

        $join = $type==1?'oversea':'activity';

        $travel = TravelModel::with($join)->where(['id'=>$travel_id])->find();
        $categorys = [];
        $titles = [];
        $fee = 0;
        foreach ($travel[$join] as $tk => $tv) {
            if($travel['type']==0){
                array_push($categorys, Activity::getCategory($tv['category']));
            }
            array_push($titles, $tv['name']);
        }
        $qrcode = qrcode($this->user['id'],url('api/travel/info',['travel_id'=>$travel_id,'parentid'=>$this->user['id']]));
        $data = [
                'id'         => $travel['id'],
                'categorys'  => implode('+',$categorys),
                'titles'     => implode('+',$titles),
                'begintime'  => $travel['begintime'],
                'endtime'    => $travel['endtime'],
                'is_promote' => $travel['is_promote'],
                'commission' => $travel['commission'],
                'qrcode'     => $qrcode,
        ];
        $userCommission = model('UserCommission')->where(['user_id'=>$this->user['id'],'travel_id'=>$travel_id])->find();
        if(!$userCommission)
        {
            model('UserCommission')->save(['user_id'=>$this->user['id'],'travel_id'=>$travel_id]);
        }
        return Json::json_send(0,'',$data);

    }

    // 收入管理
    public function income()
    {
        if(!$this->user) return Json::json_send(1);

        $data['income'] = $this->user['sum_income'];
        $data['wallet'] = $this->user['wallet_money'];

        $map['type'] = 2;
        $map['user_id'] = $this->user['id'];
        $type = input('type',0);
        if($type == 1)
        {
            $map['travel_id'] = ['<>',0];
        }
        else{
            $members = model('CommunityMember')->alias('cm')
                            ->join('user u','cm.user_id=u.id')
                            ->where(['cm.community_id'=>$this->user['id'],'paid'=>'1'])
                            ->order('entrytime desc')
                            ->select();
                $return = [];
                foreach ($members as $k => $v) {
                    $return[] = [
                        'member_id' => $v['user_id'],
                        'member_name' => $v['name'],
                        'member_avatar' => $v['avatar'],
                        'entrytime' => $v['entrytime'],
                        'fee' => $this->community['year_fee'],
                    ];
                }
                return Json::json_send(0,'',$return);
        }

         // 明细
        $details = collection(model('Paylog')->field('title,content,mode,fee,createtime')->where($map)->select())->toArray();
        $data['details'] = $details;
        return Json::json_send(0,'',$data);

        /*$commissionList = model('UserCommission')->all(['user_id'=>$this->user['id'],'state'=>1]);
        $return=[];
        foreach ($commissionList as $k => $v) {
            $travel_id = $v['travel_id'];
            $type = TravelModel::where(['id'=>$travel_id])->value('type');
            $join = $type==1?'oversea':'activity';
            $travel = TravelModel::with($join)->where(['id'=>$travel_id])->find();
            $titles = [];
            foreach ($travel[$join] as $tk => $tv) {
                array_push($titles, $tv['name']);
            }
            $return[] = [
                'travel_id' => $v['travel_id'],
                'titles' => "分销报名".implode('+',$titles),
                'sum_commission' => $v['commission']
            ];
        }*/
       /* $travel_ids = model('TravelMember')
                    ->field("travel_id,count('travel_id') as number")
                    ->where(['parentid'=>$this->user['id']])
                    ->group('travel_id')
                    ->select();
        $return=[];
        foreach ($travel_ids as $k => $v) {
            $travel_id = $v['travel_id'];
            $type = TravelModel::where(['id'=>$travel_id])->value('type');
            $join = $type==1?'oversea':'activity';
            $travel = TravelModel::with($join)->where(['id'=>$travel_id])->find();
            $titles = [];
            foreach ($travel[$join] as $tk => $tv) {
                array_push($titles, $tv['name']);
            }
            $return[] = [
                'travel_id' => $v['travel_id'],
                'titles' => "分销报名".implode('+',$titles),
                'sum_commission' => $travel['commission']*$v['number']
            ];
        }*/
        return Json::json_send(0,'',$return);
    }

    // 分销管理
    public function commission()
    {
        if(!$this->user) return Json::json_send(1);
        $commissionList = model('UserCommission')->all(['user_id'=>$this->user['id']]);
        $return=[];
        foreach ($commissionList as $k => $v) {
            $travel_id = $v['travel_id'];
            $type = TravelModel::where(['id'=>$travel_id])->value('type');
            $join = $type==1?'oversea':'activity';
            $travel = TravelModel::with($join)->where(['id'=>$travel_id])->find();
            $childCount= model('TravelMember')->where(['travel_id'=>$travel_id,'paid'=>'1','parentid'=>$this->user['id']])->count();
            $titles = [];
            foreach ($travel[$join] as $tk => $tv) {
                array_push($titles, $tv['name']);
            }
            $return[] = [
                'travel_id' => $v['travel_id'],
                'titles' => implode('+',$titles),
                'memebr_price' => $travel['memebr_price'],
                'commission' => $travel['commission'],
                'state' => $v['state'],
                'count' => $childCount,
                'sum_commission'=>$childCount * $travel['commission']
            ];
        }
        return Json::json_send(0,'',$return);
    }

     /**
     * 插入聊天记录
     */
    public function chat_history()
    {
        $list = getChatHistory();
        foreach($list as $value)
        {
           $chat = Db::name('qz_chat')->where('msg_id',$value['msg_id'])->find();
           // 根据 msg_id判断此聊天记录是否存在，如果存在不插入，存在则插入数据库
           if(!$chat)
           {
              $value['payload'] = json_encode($value['payload'],JSON_UNESCAPED_UNICODE);
              Db::name('qz_chat')->insert($value);
              Log::info('聊天记录', $value['msg_id'].'插入成功');
           }
        }
    }
} 
